<?php

namespace app\model;

use app\common\LoginType;
use app\traits\JwtAuthModelTrait;
use app\traits\ModelTrait;
use think\Model;

class User extends Model
{
    use ModelTrait;
    use JwtAuthModelTrait;

    public array $notes = [
        'status'     => [
            1 => '正常',
            2 => '禁用',
        ],
        'is_vip'     => [
            1 => 'VIP',
            2 => '非VIP',
        ],
        'sex'        => [
            1 => '男',
            0 => '女',
        ],
        'vip_status' => [
            1 => '正常',
            2 => '已过期',
            3 => '即将过期',
        ],
    ];

    /**
     * @param int $uid
     * @return int
     */
    public static function is_really_vip(int $uid = 0): int
    {
        if (!$uid) return 0;
        $vip_info = self::get_vip_info($uid);
        if ($vip_info) return $vip_info['vip_type'];
        return 0;
    }

    public static function wechat_app_register($unionid, $wxOpenid, $mobile, $headimgurl, $nickname, $sex, string $ref = '')
    {
        try {
            $date = myDate();
            $data = [
                'status'     => 1,
                'create_time'     => $date,
                'date_v'     => $date,
                'mobile'     => $mobile,
                'unionid'    => $unionid,
                'wx_openid'  => $wxOpenid,
                'avatar'     => $headimgurl,
                'nickname'   => $nickname,
                'sex'        => $sex,
                'login_type' => LoginType::LOGIN_TYPE_WECHATAPP->value,
                'ref'        => $ref ?: request()->session()->get('ref', ''),
                'from'       => request()->host(true),
                'os_type'    => request()->os_type,
            ];

            self::create($data);
            $where[] = ['wx_openid', '=', $wxOpenid];
            $user    = self::where($where)->find();

            $param = ['uid' => $user['id'], 'type' => 1, 'user_status' => 1, 'status' => 1, 'login_type' => LoginType::LOGIN_TYPE_WECHAT->value];
            UserBehaviorLog::log($param);
            return ['code' => 200, 'data' => $user];
        } catch (\Throwable $exception) {
            return ['code' => 400, 'msg' => $exception->getMessage()];
        }
    }

    public static function mobile_register($mobile = '', $field = '*', $ref = '')
    {
        if (empty($mobile)) return false;
        try {
            $date = myDate();
            $data = [
                'status'     => 1,
                'create_time'     => $date,
                'date_v'     => $date,
                'mobile'     => $mobile,
                'nickname'   => substr($mobile, 0, 3) . '****' . substr($mobile, -4),
                'login_type' => LoginType::LOGIN_TYPE_PHONE->value,
                'ref'        => $ref ?: request()->session()->get('ref', ''),
                'from'       => request()->host(true),
                'os_type'    => request()->os_type,
            ];
            self::create($data);
            $where[] = ['mobile', '=', $mobile];
            $user    = self::where($where)->field($field)->find();
            $param   = ['uid' => $user['id'], 'type' => 1, 'user_status' => 1, 'status' => 1, 'login_type' => LoginType::LOGIN_TYPE_PHONE->value];
            UserBehaviorLog::log($param);
            return $user;
        } catch (\Throwable $exception) {
            return false;
        }
    }

    public static function wechat_register(array $userInfo, string $ref = '')
    {
        $unionid = $userInfo['unionid'] ?? '';
        if (empty($unionid)) return false;
        try {
            $date = myDate();
            $data = [
                'status'     => 1,
                'create_time'     => $date,
                'date_v'     => $date,
                'mobile'     => isset($userInfo['mobile']) ? $userInfo['mobile'] : '',
                'unionid'    => $unionid,
                'avatar'     => $userInfo['headimgurl'],
                'nickname'   => $userInfo['nickname'],
                'login_type' => LoginType::LOGIN_TYPE_WECHAT->value,
                'ref'        => $ref ?: request()->session()->get('ref', ''),
                'from'       => request()->host(true),
                'os_type'    => request()->os_type,
            ];
            self::create($data);
            $where[] = ['unionid', '=', $unionid];
            $user    = self::where($where)->find();
            $param   = ['uid' => $user['id'], 'type' => 1, 'user_status' => 1, 'status' => 1, 'login_type' => LoginType::LOGIN_TYPE_WECHAT->value];
            UserBehaviorLog::log($param);
            return $user;
        } catch (\Throwable $exception) {
            return false;
        }
    }

    public static function qq_register(array $userInfo, $ref = '')
    {
        $unionid = $userInfo['unionid'] ?? '';
        if (empty($unionid)) return false;
        try {
            $date = myDate();
            $data = [
                'status'     => 1,
                'create_time'     => $date,
                'date_v'     => $date,
                'mobile'     => '',
                'qq_unionid' => $unionid,
                'avatar'     => $userInfo['figureurl_qq_1'],
                'nickname'   => $userInfo['nickname'],
                'login_type' => LoginType::LOGIN_TYPE_QQ->value,
                'ref'        => $ref ?: request()->session()->get('ref', ''),
                'from'       => request()->host(true),
                'os_type'    => request()->os_type,
            ];
            self::create($data);
            $where[] = ['qq_unionid', '=', $unionid];
            $user    = self::where($where)->find();
            $param   = ['uid' => $user['id'], 'type' => 1, 'user_status' => 1, 'status' => 1, 'login_type' => LoginType::LOGIN_TYPE_QQ->value];
            UserBehaviorLog::log($param);
            return $user;
        } catch (\Throwable $exception) {
            return false;
        }
    }

    public static function weibo_register(array $userInfo, $ref = '')
    {
        $unionid = $userInfo['id'] ?? '';
        if (empty($unionid)) return false;
        try {
            $date = myDate();
            $data = [
                'status'        => 1,
                'create_time'        => $date,
                'date_v'        => $date,
                'mobile'        => '',
                'weibo_unionid' => $unionid,
                'avatar'        => $userInfo['avatar_hd'],
                'nickname'      => $userInfo['screen_name'],
                'login_type'    => LoginType::LOGIN_TYPE_WEIBO->value,
                'ref'           => $ref ?: request()->session()->get('ref', ''),
                'from'          => request()->host(true),
                'os_type'       => request()->os_type,
            ];
            self::create($data);
            $where[] = ['weibo_unionid', '=', $unionid];
            $user    = self::where($where)->find();
            $param   = ['uid' => $user['id'], 'type' => 1, 'user_status' => 1, 'status' => 1, 'login_type' => LoginType::LOGIN_TYPE_WEIBO->value];
            UserBehaviorLog::log($param);
            return $user;
        } catch (\Throwable $exception) {
            return false;
        }
    }

    public static function apple_register(array $userInfo, string $ref = '')
    {
        $apple_sub = $userInfo['sub'] ?? '';
        if (empty($apple_sub)) return false;
        try {
            $date = myDate();
            $data = [
                'status'     => 1,
                'create_time'     => $date,
                'date_v'     => $date,
                'mobile'     => '',
                'apple_sub'  => $apple_sub,
                'avatar'     => '',
                'nickname'   => $userInfo['nickname'],
                'login_type' => LoginType::LOGIN_TYPE_APPLE->value,
                'ref'        => $ref ?: request()->session()->get('ref', ''),
                'from'       => request()->host(true),
                'os_type'    => request()->os_type,
            ];
            self::create($data);
            $where[] = ['apple_sub', '=', $apple_sub];
            $user    = self::where($where)->find();
            $param   = [
                'uid'         => $user['id'],
                'type'        => 1,
                'user_status' => 1,
                'status'      => 1,
                'login_type'  => LoginType::LOGIN_TYPE_APPLE->value
            ];
            UserBehaviorLog::log($param);
            return $user;
        } catch (\Throwable $exception) {
            return false;
        }
    }

    /**
     * 游客登录
     *
     * @param string $uuid
     * @return array
     */
    public function touristLogin(string $uuid): array
    {
        try {
            $where1[] = ['uuid', '=', $uuid];
            $user     = User::where($where1)->find();
            //是否是新用户 1是
            $is_new_user = 0;
            if (empty($user)) {
                $date = myDate();
                $data = [
                    'status'     => 1,
                    'create_time'     => $date,
                    'date_v'     => $date,
                    'mobile'     => '',
                    'uuid'       => $uuid,
                    'avatar'     => '',
                    'nickname'   => '游客' . date('ymd'),
                    'login_type' => LoginType::LOGIN_TYPE_TOURIST->value,
                    'from'       => request()->host(true),
                    'os_type'    => request()->os_type,
                ];
                self::create($data);

                $user  = User::where($where1)->find();
                $param = [
                    'uid'         => $user['id'],
                    'type'        => 1,
                    'user_status' => 1,
                    'status'      => 1,
                    'login_type'  => LoginType::LOGIN_TYPE_TOURIST->value
                ];
                UserBehaviorLog::log($param);
                $is_new_user = 1;
            }

            return ['code' => 200, 'data' => $user, 'is_new_user' => $is_new_user];
        } catch (\Throwable  $exception) {
            return ['code' => 400, 'msg' => $exception->getMessage()];
        }
    }

}